package com.bot.adms.controller.ad;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.vo.AdInfoVo;
import com.bot.adms.service.impl.AdServiceImpl;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 广告预览控制器
 * 根据广告ID获取广告预览信息，包含标题、内容和预览URL
 * 请求路径: /previewAd
 * 请求方式: GET
 * @author FangZ
 */
@WebServlet("/PreviewAd")
public class PreviewServlet extends HttpServlet {
    // 将AdServiceImpl声明为类成员变量
    private AdServiceImpl adService;
    
    // 在构造方法中初始化AdServiceImpl
    public PreviewServlet() {
        this.adService = new AdServiceImpl();
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //修改字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //响应体创建
        JSONObject jsonObject = new JSONObject();
        boolean success = false;
        int code = 500;
        String msg = "预览失败";
        JSONObject data = new JSONObject();

        //1.获取广告id参数
        String id = req.getParameter("id");
        //2.参数验证
        if (id == null || id.trim().isEmpty()){
            jsonObject.put("success", false);
            jsonObject.put("code", 500);
            jsonObject.put("msg", "id不存在");
            
            //转换成JSON字符串
            String json = JSON.toJSONString(jsonObject);
            
            //响应字符编码为UTF-8
            resp.setCharacterEncoding("utf-8");
            //设置响应内容类型为JSON格式
            resp.setContentType("application/json;charset=utf-8");
            //获取输出流并将JSON数据写入响应
            ServletOutputStream outputStream = resp.getOutputStream();
            outputStream.write(json.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();
            return;
        }

        try {
            //3.将字符串类型的id转换成Long类型
            Long idLong = Long.parseLong(id);
            //4.调用业务逻辑，使用类成员变量
            //5.执行广告预览查询
            AdInfoVo adInfo = adService.previewAd(idLong);

            //6.响应结果
            if (adInfo != null) {
                success = true;
                code = 200;
                msg = "预览成功";
                data.put("adInfo", adInfo);
            } else {
                success = false;
                code = 500;
                msg = "未找到广告预览信息";
            }
        } catch (NumberFormatException e) {
            success = false;
            code = 500;
            msg = "广告ID格式错误，必须为数字";
        } catch (Exception e) {
            success = false;
            code = 500;
            msg = "预览失败: " + e.getMessage();
        }
        
        //组装JSON
        jsonObject.put("success", success);
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        //转换成JSON字符串
        String json = JSON.toJSONString(jsonObject);

        //响应字符编码为UTF-8
        resp.setCharacterEncoding("utf-8");
        //设置响应内容类型为JSON格式
        resp.setContentType("application/json;charset=utf-8");
        //获取输出流并将JSON数据写入响应
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(json.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}